TablesExtension.renderTable   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 11
c 0
b 0
f 0
rs 9.95
cc 1
1
import { Extension } from 'nunjucks';
2
import { ITemplates } from '../ITemplates';
3
import { Inline, Table } from 'src/Infrastructure/Tables';
4
5
// See: https://mozilla.github.io/nunjucks/api.html#custom-tags
6
export class TablesExtension implements Extension {
7
  public readonly tags = ['table', 'inlinetable'];
8
9
  constructor(private readonly templates: ITemplates) {}
10
11
  public parse(parser: any, nodes: any, _lexer: any) {
12
    const tagToken = parser.nextToken();
13
14
    const args = parser.parseSignature(null, true);
15
    parser.advanceAfterBlockEnd(tagToken.value);
16
17
    const methodName =
18
      tagToken.value === 'table' ? 'renderTable' : 'renderInlineTable';
19
20
    return new nodes.CallExtension(this, methodName, args, []);
21
  }
22
23
  public renderTable(
24
    _context: object,
25
    table: Table,
26
    extraContext: object = {}
27
  ) {
28
    const html = this.templates.render('tables/table.njk', {
29
      table,
30
      ...extraContext
31
    });
32
    return this.templates.markSafe(html);
33
  }
34
35
  public renderInlineTable(
36
    _context: object,
37
    inline: Inline,
38
    extraContext: object = {}
39
  ) {
40
    const html = this.templates.render('tables/inlinetable.njk', {
41
      inline,
42
      ...extraContext
43
    });
44
    return this.templates.markSafe(html);
45
  }
46
}
47